package com.zhongwang.mms.module.half.controller;

import com.zhongwang.mms.base.BaseController;
import com.zhongwang.mms.common.JsonResult;
import com.zhongwang.mms.gen.entity.HChangeOrder;
import com.zhongwang.mms.gen.entity.HInventory;
import com.zhongwang.mms.gen.entity.User;
import com.zhongwang.mms.module.half.dao.HChangeOrderDao;
import com.zhongwang.mms.module.half.dao.HInventoryDao;
import com.zhongwang.mms.module.half.model.HChangeOrderModel;
import com.zhongwang.mms.module.half.model.HInventoryQueryModel;
import com.zhongwang.mms.module.half.service.ChangeApplyService;
import com.zhongwang.mms.module.purchase.model.ChangeOrderQueryModel;
import com.zhongwang.mms.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @description:串料申请/执行
 * @author: th
 * @time: 2020/4/25 10:03
 */
@Controller
@RequestMapping("/half/halInvManage/changeApply")
public class    ChangeApplyController extends BaseController {
    //页面文件路径
    private String prefix = "half/halInvManage/changeApply";
    @Autowired
    private ChangeApplyService changeApplyService;
    @Autowired
    private HInventoryDao hInventoryDao;
    @Autowired
    private HChangeOrderDao hChangeOrderDao;

    /**
     * 加载页面
     * @param model
     * @return
     */
    @GetMapping("/page")
    @RequiresPermissions({"halinv:halchalist:view"})
    public String create(Model model) {

        // 获取登入用户实体
        User user = ShiroUtils.getUser();
        model.addAttribute("userName", user.getName());
        model.addAttribute("odrDate", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        return prefix + "/page";
    }

    /**
     * 加载页面列表数据
     * @param queryModel
     * @return
     */
    @ResponseBody
    @PostMapping("/list")
    @RequiresPermissions({"halinv:halchalist:view"})
    public JsonResult list(ChangeOrderQueryModel queryModel) {

        startPage();
        return jsonResult(changeApplyService.selectChangeOrderListByQuery(queryModel));
    }

    /**
     * 跳转新建串料申请页面
     * @param model
     * @return
     */
    @GetMapping("/createPage")
    public String createPage(Model model) {

        // 获取登入用户实体
        User user = ShiroUtils.getUser();
        model.addAttribute("userName", user.getName());
        model.addAttribute("odrDate", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        return prefix + "/createPage";
    }

    /**
     * 弹出框数据加载
     * @param query
     * @return
     */
    @ResponseBody
    @PostMapping("/inventoryList")
    @RequiresPermissions({"halinv:halchalist:view"})
    public JsonResult inventoryList(HInventoryQueryModel query) {

        startPage();
        return jsonResult(changeApplyService.inventoryList(query));
    }

    /**
     * 弹出框点击选择按钮根据ityId查询库存信息
     * @param ityId
     * @return
     */
    @ResponseBody
    @PostMapping("/getHInventory")
    @RequiresPermissions({"halinv:halchalist:view"})
    public JsonResult getHInventory(Integer ityId) {

        HChangeOrder hChangeOrder = changeApplyService.getHInventoryByItyId(ityId);
        return JsonResult.success("加载成功！", hChangeOrder);
    }

    /**
     * 保存新增串料
     * @param hChangeOrder
     * @return
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions({"halinv:halchalist:edit"})
    public JsonResult save(HChangeOrder hChangeOrder) {

        return jsonResult(changeApplyService.saveChangeOrder(hChangeOrder));
    }

    /**
     * 提交串料申请
     * @param hChangeOrder
     * @return
     */
    @ResponseBody
    @PostMapping("/submitApply")
    @RequiresPermissions({"halinv:halchalist:edit"})
    public JsonResult submitApply(HChangeOrder hChangeOrder) {

        return jsonResult(changeApplyService.submitApply(hChangeOrder));
    }

    /**
     * 删除串料申请
     * @param hChangeOrder
     * @return
     */
    @ResponseBody
    @PostMapping("/removeApply")
    @RequiresPermissions({"halinv:halchalist:edit"})
    public JsonResult removeApply(HChangeOrder hChangeOrder) {

        return jsonResult(changeApplyService.removeApply(hChangeOrder));
    }

    /**
     *查看
     * @param model
     * @param mode
     * @param ceoId
     * @return
     */
    @GetMapping("/viewPage/{mode}/{ceoId}")
    @RequiresPermissions({"halinv:halchalist:view"})
    public String viewPage(Model model,@PathVariable("mode") String mode, @PathVariable("ceoId") Integer ceoId) {

        HChangeOrderModel hChangeOrderModel = hChangeOrderDao.getAllByPrimary(ceoId);
        HInventory hInventory = hInventoryDao.selectAllById(hChangeOrderModel.getCeoUseItyId());
        model.addAttribute("mode", mode);
        model.addAttribute("ceoId", hChangeOrderModel.getCeoId());
        model.addAttribute("ceoAppPerson", hChangeOrderModel.getCeoAppPerson());
        model.addAttribute("ceoAppPersonId", hChangeOrderModel.getCeoAppPersonId());
        model.addAttribute("ceoAppDate", new SimpleDateFormat("yyyy-MM-dd").format(hChangeOrderModel.getCeoAppDate()));
        model.addAttribute("ceoNeedProCode",hChangeOrderModel.getCeoNeedProCode());
        model.addAttribute("ceoNeedProName",hChangeOrderModel.getCeoNeedProName());
        model.addAttribute("ceoNeedMatName",hChangeOrderModel.getCeoNeedMatName());
        model.addAttribute("ceoNeedNum",hChangeOrderModel.getCeoNeedNum());
        model.addAttribute("ceoNeedUnit",hChangeOrderModel.getCeoNeedUnit());
        model.addAttribute("ceoUseProCode",hChangeOrderModel.getCeoUseProCode());
        model.addAttribute("ceoUseProName",hChangeOrderModel.getCeoUseProName());
        model.addAttribute("ceoUseMatName",hChangeOrderModel.getCeoUseMatName());
        model.addAttribute("ceoUseNum",hInventory.getItyAvaNum() + hChangeOrderModel.getCeoNeedNum());
        model.addAttribute("ceoUseUnit",hChangeOrderModel.getCeoUseUnit());
        return prefix + "/createPage";
    }

    /**
     * 根据主键获取串料信息
     * @param hChangeOrderModel
     * @return
     */
    @ResponseBody
    @PostMapping("/getChangeOrder")
    @RequiresPermissions({"halinv:halchalist:view"})
    public JsonResult getChangeOrder(HChangeOrderModel hChangeOrderModel) {

        return JsonResult.success("",changeApplyService.getChangeModel(hChangeOrderModel.getCeoIds()));
    }
}
